package com.tuanzi.loan.business.repository.credit.impl;

import com.tuanzi.loan.business.repository.credit.LoanCreditOrderRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.credit.LoanCreditOrderRequest;
import com.tuanzi.loan.vo.response.credit.LoanCreditOrderResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanCreditOrder.LOAN_CREDIT_ORDER;

/**
 * User: liangZongXi
 * Create time: 11:28 2019/9/17
 * Description:
 */
@Repository
public class LoanCreditOrderRepositoryImpl extends JOOQBaseRepository implements LoanCreditOrderRepository {

    @Override
    public Page<LoanCreditOrderResponse> list(LoanCreditOrderRequest searchRequest, Pageable pageable) {
        fields(LOAN_CREDIT_ORDER.fields());
        fields(COMM_PRODUCT_PRDID.PARENT_ID, COMM_PRODUCT_PRDID.PRDID);

        leftJoin(COMM_PRODUCT_PRDID, LOAN_CREDIT_ORDER.PRODUCT_ID.eq(COMM_PRODUCT_PRDID.PRDID.cast(String.class)));

        ge(LOAN_CREDIT_ORDER.CREATE_DATE, searchRequest.getBeginDate());
        le(LOAN_CREDIT_ORDER.CREATE_DATE, searchRequest.getEndDate());
        eq(LOAN_CREDIT_ORDER.PRODUCT_ID, searchRequest.getProductId());
        eq(COMM_PRODUCT_PRDID.PARENT_ID, searchRequest.getParentId());
        eq(LOAN_CREDIT_ORDER.STATUS, searchRequest.getStatus());
        eq(LOAN_CREDIT_ORDER.PLATFORM, searchRequest.getPlatform());
        eq(LOAN_CREDIT_ORDER.PAY_TYPE, searchRequest.getPayChannel());
        contains(LOAN_CREDIT_ORDER.CHANNEL, searchRequest.getChannel());
        contains(LOAN_CREDIT_ORDER.ENTRANCE, searchRequest.getEntrance());
        orderBy(LOAN_CREDIT_ORDER.CREATE_TIME, Sort.Direction.DESC);

        return search(pageable, record -> {
            LoanCreditOrderResponse response = new LoanCreditOrderResponse();
            response.setCreateTime(record.get(LOAN_CREDIT_ORDER.CREATE_DATE));
            response.setPayTime(record.get(LOAN_CREDIT_ORDER.PAY_TIME));
            response.setParentId(record.get(COMM_PRODUCT_PRDID.PARENT_ID));
            response.setProductId(record.get(LOAN_CREDIT_ORDER.PRODUCT_ID));
            response.setPlatform(record.get(LOAN_CREDIT_ORDER.PLATFORM));
            Long channelLong = record.get(LOAN_CREDIT_ORDER.CHANNEL);
            response.setChannel(null != channelLong ? channelLong.toString() : null);
            Byte payChannel = record.get(LOAN_CREDIT_ORDER.PAY_TYPE);
            response.setPayChannel(payChannel == null ? null : payChannel.intValue());
            response.setOrderId(record.get(LOAN_CREDIT_ORDER.ORDER_ID));
            response.setOrderStatus(record.get(LOAN_CREDIT_ORDER.STATUS).toString());
            response.setEntrance(record.get(LOAN_CREDIT_ORDER.ENTRANCE));
            response.setOrderNum(record.get(LOAN_CREDIT_ORDER.AMOUNT));
            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_CREDIT_ORDER;
    }
}
